<?php
/**
 * @author Feng
 * @date 2012-6-5
 */
class controller_admin_collection_collect extends system_user_controller_collection {
	
	public function action_index() {
	
	}
	
	public function action_item() {
		$id = intval($this->_param('id'));
		$concept = new collection_item_concept(array('lang' => $this->user->getLang()), array('lang = ?' => $this->user->getLang()));
		if(!($row = $concept->fetch(array('item.id = ?' => $id)))) {
			$this->_error("This Item is not Existing");
		}
		if(!$row->getIsActive()) {
			$this->_error("This Item is Invalid.");
		}

		$params = $row->getParams()->toArray();
		if($this->a == 'collect-special') {
			$this->collectSpecial($row);
			$this->_output();
		}
		
		if($this->a == 'collect-photo') {
			$concept = new collection_special_concept(array('item_id' => $row->getId()), array('item_id = ?' => $row->getId()));
			$allCount = $concept->fetchCount();
			if(!($spage = intval($this->_post('spage'))) || $spage < 0) {
				$spage = 1;
			}
			if($spage > $allCount) {
				$this->_data->complete = true;
				$this->_data->params = array('action' => 'complete');
				$this->_data->message = "COMPLETE ..";
				$this->_output();
			}
			foreach($concept->fetchAll(array(), 'special.id ASC', 1, $spage) AS $special) {
				$this->collectPhoto($special, $spage);
				$this->_output();
			}		
		}
		
		if($this->a == 'complete') {
			$this->_data->complete = true;
			$this->_data->message = "ALL COMPLETE!";
			$this->_output();
		}

		$params = empty($params) ? array('action' => 'collect-special') : $params;
		$error = array();
		$this->_data->complete = false;
		$this->_data->message = "INITIALIZE {$this->bold($row->getTitle())} ...";
		if($this->_post('first_time')) {
			try {
				foreach((array) $this->_post('category_id') as $cid) {
					$row->addCategoryList($cid);
				}
				$row->save();
			} catch (Exception $e) {
				$msg = "AN ERROR OCCURRED： {$this->red($e->getMessage())}";
				if($this->getSetting('ignore_error')) {
					$error = array('error' => $e->getMessage());
					$this->addMessage($msg);
				} else {
					$this->_error($msg);
				}
			}
			if($this->getSetting('from_last_position')) {
				$this->_data->params = $params;
				$this->addMessage("STARTED FROM LAST POSITION ..");
			} else {
				$this->_data->params = array('action' => 'collect-special');
			}
		} else {
			if($this->getSetting('from_last_position')) {
				$this->_data->params = $params;
				$this->addMessage("STARTED FROM LAST POSITION ..");
			} else {
				$this->_data->params = array('action' => 'collect-special');
			}
		}
		$this->_data->params = array_merge((array) $this->_data->params, $error);
		$this->_output();
	}
	
	public function action_special() {
		
	}

}
